package ant;


import static org.red5.server.api.ScopeUtils.getScopeService;

import java.util.ArrayList;
import java.util.HashMap;

import org.red5.server.api.so.ISharedObject;
import org.red5.server.api.so.ISharedObjectService;
import org.red5.server.so.SharedObjectService;

import com.unicate.DrawObject;
import com.woa.Application;


//keep trace of objects by id.
public class WOAObjectListner {

	public HashMap<Integer, WOAObject> all_objects;
	public HashMap<Integer, ArrayList<WOAObject>> all_objects_screen;
	private Integer generated_id = 0;
	public Application appli;
	public ISharedObject so;
	
	public WOAObjectListner(Application a)
	{
		this.appli = a;
		all_objects = new HashMap<Integer, WOAObject>();
		all_objects_screen = new HashMap<Integer, ArrayList<WOAObject>>();
	}
	
	public Integer get_new_id()
	{
		generated_id++;
		return generated_id;
	}
	
	public void add_o(WOAObject o)
	{
		all_objects.put(o.id, o);
		all_objects_screen.get(o.pos._map).add(o);
	}
	public WOAObject get_o(Integer id)
	{
		return all_objects.get(id);
	}

	public DrawObject getDrawObject(Integer id)
	{
		WOAObject dobject = all_objects.get(id);
		return dobject.getDrawObject();
	}
	
	public void woaobject_update()
	{
		/*
		Object dynamic = so.getAttribute("dynamics");
		if (dynamic != null)
		{
			ArrayList<DrawObject> da = (ArrayList<DrawObject>)dynamic;
			so.beginUpdate();
			ArrayList<WOAObject> woa_list = all_objects_screen.get(0);
			//for (ArrayList<WOAObject> woa_list : all_objects_screen.get(0))
			{
				for (WOAObject woa : woa_list)
				{
					//da.add(woa.id);
					appli.log.debug("updating " + woa.id + " for dynamics");
					da.set(woa.id, woa.getDrawObject());
					//msg += woa.id + ".";
				}					
			}
			
		}
		*/
		//this.so.beginUpdate();
		
		//
		HashMap<Integer, DrawObject> da = new HashMap<Integer, DrawObject>();
		//String msg = "empty ";
		//ArrayList<Integer> da = new ArrayList<Integer>();
		if (all_objects_screen != null && all_objects_screen.size() > 0 )				
		{
			ArrayList<WOAObject> woa_list = all_objects_screen.get(0);
			appli.log.debug("updating : size for screen 0 : " + woa_list.size());	
			for (WOAObject woa : woa_list)
			{
				//da.add(woa.id);
				if (woa != null)
				{
					//appli.log.debug("woa id : " + woa.id);	
					da.put(woa.id, woa.getDrawObject());							
				}
				
				//msg += woa.id + ".";
			}
			//so.setAttribute("message", msg);
		}
		if (da != null)
		{				
			appli.log.debug("dynamics size is :" + da.size());
			so.setAttribute("dynamics", da);			
		}
		else
		{
			appli.log.debug("da is null");	
		}
		//so.endUpdate();
		
		
	}
	
	public void woaobject_screen_share()
	{
		if (appli.appScope != null)
		{				
	      ISharedObjectService service = (ISharedObjectService) getScopeService(
	    		  appli.appScope,
	              ISharedObjectService.class,
	              SharedObjectService.class,
	              false
	        );				
			
	        service.createSharedObject(appli.appScope, "woalshare", false);
			so = service.getSharedObject(appli.appScope, "woalshare", false);	
			appli.log.debug("share world has been setup");	
		    //so.setAttribute("time", worldtime);
		    //appli.log.debug("timer shared object start to " + so.getAttribute("time").toString());
			
			try
			{
				//woaobject_update();
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			
			
			
			/*
			HashMap<Integer, DrawObject> da = new HashMap<Integer, DrawObject>();
			//String msg = "empty ";
			//ArrayList<Integer> da = new ArrayList<Integer>();
			if (all_objects_screen != null && all_objects_screen.size() > 0 )				
			{
				ArrayList<WOAObject> woa_list = all_objects_screen.get(0);
				appli.log.debug("size for screen 0 : " + woa_list.size());	
				for (WOAObject woa : woa_list)
				{
					//da.add(woa.id);
					if (woa != null)
					{
						//appli.log.debug("woa id : " + woa.id);	
						da.put(woa.id, woa.getDrawObject());							
					}
					
					//msg += woa.id + ".";
				}
				//so.setAttribute("message", msg);
			}
			if (da != null)
			{				
				appli.log.debug("dynamics size is :" + da.size());
				so.setAttribute("dynamics", da);			
			}
			*/
		}
		else
		{
			appli.log.debug("app scope is null under woaobject share");	
		}		
	}
	
	public void remove_o(Integer id)
	{
		WOAObject r = all_objects.get(id);
		all_objects_screen.get(r.pos._map).remove(r);
		all_objects.remove(id);
	}
}
